package cn.dnui_wwt524.dnuifood_16110100524.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_wwt524.dnuifood_16110100524.Adapter.CommentsAdapter;
import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.CollectStatus;
import cn.dnui_wwt524.dnuifood_16110100524.bean.FoodBean;
import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.model.FoodInfoModel;
import cn.dnui_wwt524.dnuifood_16110100524.model.UserModel;
import cn.dnui_wwt524.dnuifood_16110100524.service.FoodService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodInfoActivity extends AppCompatActivity implements OnRetrofitListener {
    private Context context;
    private String food_id;
    private TextView food_name, food_price, food_inro;
    private ImageView food_img;
    private FoodBean foodInfo;
    private List<FoodBean> commentsInfo;
    private CommentsAdapter commentsAdapter;
    private Button shopinfo_goumai;
    private RecyclerView recyclerView;
    private Menu menu;
    String userid;

    public FoodInfoActivity() {
        this.context = context;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_info);
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo", MODE_PRIVATE);
        userid = (String)sharedPreferences.getString("userid", "");
        food_id = getIntent().getStringExtra("food_id");

        initViews();
        initData(food_id);
    }

    void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.comments_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        food_name = (TextView) findViewById(R.id.foodinfo_foodname);
        food_inro = (TextView) findViewById(R.id.foodinfo_intro);
        food_price = (TextView) findViewById(R.id.foodinfo_price);
        food_img = (ImageView) findViewById(R.id.foodinfo_image);
        shopinfo_goumai = findViewById(R.id.shopinfo_goumai);
    }

    void initData(String food_id) {
        FoodInfoModel foodInfoModel = new FoodInfoModel();
        foodInfoModel.getFoodById(food_id, this);//访问服务器请求
        foodInfoModel.getAllCommentsByFood(food_id,this);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.menu_collect,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        iscollected();
        return super.onCreateOptionsMenu(menu);
    }

    private void iscollected(){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        FoodService foodService = retrofit.create(FoodService.class);

        Call<CollectStatus> call = foodService.isCollected(userid,food_id,"1");
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getCollected().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("取消收藏");
                }else {
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(FoodInfoActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        //实现"显示收藏还是取消收藏"
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    //收藏店铺
                    collectShop();
                }
                else
                {
                    //取消收藏
                    uncollectShop();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void collectShop() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        FoodService foodService = retrofit.create(FoodService.class);

        Call<CollectStatus> call = foodService.userCollectFood(userid,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("取消收藏");
                    Toast.makeText(FoodInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(FoodInfoActivity.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }

    private void uncollectShop() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        FoodService foodService = retrofit.create(FoodService.class);

        Call<CollectStatus> call = foodService.userCollectFood(userid,food_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    Toast.makeText(FoodInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(FoodInfoActivity.this, "取消收藏失败", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }


    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag) {
            case 1:
                this.foodInfo = (FoodBean) object;
                food_name.setText(foodInfo.getFoodname());
                food_price.setText(foodInfo.getPrice() + "元");
                final String str1 = foodInfo.getPrice() + "元";
                food_inro.setText(foodInfo.getIntro());
                Picasso.with(context).load(UserModel.Constants.BASE_URL + foodInfo.getPic()).into(food_img);
                shopinfo_goumai.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(FoodInfoActivity.this, BuyActivity.class);
                        intent.putExtra("shop_id", foodInfo.getShop_id());
                        intent.putExtra("food_id", foodInfo.getFood_id());
                        intent.putExtra("food_name", foodInfo.getFoodname());
                        intent.putExtra("price",str1);
                        startActivity(intent);
                    }
                });
                break;

            case 2:
                commentsInfo = (List<FoodBean>) object;
                commentsAdapter = new CommentsAdapter(FoodInfoActivity.this, commentsInfo, R.layout.foodinfo_item);
                recyclerView.setAdapter(commentsAdapter);
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(this, "网络连接失败", Toast.LENGTH_SHORT).show();
    }
}
